
<template>
  <el-row class="row-bg alignment-container" justify="center" align="middle">
    <div class="common-layout">

      <el-container>

        <el-header height="120px">
          <el-row class="row-bg" justify="center">
            <el-col >
            </el-col>
          </el-row>

        </el-header>
        <el-main>
          <el-row class="row-bg" justify="center">
            <div style="margin: 20px" />

            <el-form :label-position="labelPosition" label-width="100px" :model="formLabelAlign" style="max-width: 460px">
              <el-text class="mx-1">用户名:1 密码:1</el-text>
              <el-form-item label="用户名">
                <el-input v-model="formLabelAlign.user" />
              </el-form-item>
              <el-form-item label="密码">
                <el-input v-model="formLabelAlign.password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm(formLabelAlign)">
                  登陆
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">click</el-button>
              </el-form-item>
            </el-form>
          </el-row>
        </el-main>

      </el-container>

    </div>

  </el-row>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { inject } from 'vue';
import { useRouter, } from 'vue-router'

import { use_login_store } from '/src/stores/login.js'

const login_info=use_login_store();

const labelPosition = ref('top')

const formLabelAlign = reactive({
  user: '',
  password: ''
})
const router = useRouter();

const sg_oss = inject('h');

// const user = localStorage.getItem(`user`);

// if (user != null) {
//   router.back();
// }
if (login_info.get_token() != "") {
  router.back();
}

const submitForm = async (formEl) => {
  // if (!formEl) return
  // const ok = await formEl.validate((valid, fields) => {
  //   if (valid) {
  //     console.log('submit!')
  //   } else {
  //     console.log('error submit!', fields)
  //   }
  // })
  // if (ok) {
  const res = await fetch(`${sg_oss}/f1/user/doLogin`, {
    method: "post",
    headers: {
      "Content-Type": "application/json",
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: JSON.stringify(formLabelAlign)
  })

  if (res.ok) {
    const s = await res.json();
    if (s.code == `200`){
      // localStorage.setItem("user", JSON.stringify({ token: s.data }));
      login_info.set_token(s.data);
    }
    console.log("登陆成功");
    let tox = router.currentRoute.value.query.to;
    if (tox == null || tox == ``) {
      tox = `/front`;
    }
    router.replace(tox);
  }

  // }
}
</script>


